//
//  CNNewController.m
//  BeeQuick_19
//
//  Created by 刘家强 on 16/9/23.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNNewController.h"
#import "CNNewCell.h"

@interface CNNewController ()

@property (nonatomic, strong) NSArray *imagesArr;
@property (nonatomic, weak) UIPageControl *pages;

@end

@implementation CNNewController


- (instancetype)init
{
    self = [super init];
    if (self) {
        
        // 设置layout
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        
        // 设置item
        layout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        self = [self initWithCollectionViewLayout:layout];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置背景颜色
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"CNNewCell" bundle:nil] forCellWithReuseIdentifier:@"NewCell"];
    self.imagesArr = @[@"guide_40_1", @"guide_40_2", @"guide_40_3", @"guide_40_4"];
    
    // 设置相关参数
    self.collectionView.bounces = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.pagingEnabled = YES;
    
    // 创建pags
    UIPageControl *pages = [[UIPageControl alloc] init];
    [self.view addSubview:pages];
    [pages mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.offset(-20);
    }];
    pages.pageIndicatorTintColor = [UIColor greenColor];
    pages.currentPageIndicatorTintColor = [UIColor whiteColor];
    pages.numberOfPages = self.imagesArr.count;
    pages.currentPage = 0;
    
    // 禁用交互
    pages.userInteractionEnabled = NO;
    self.pages = pages;
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imagesArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    CNNewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NewCell" forIndexPath:indexPath];
    
    cell.imageView.image = [UIImage imageNamed:self.imagesArr[indexPath.row]];
    cell.nextButton.hidden = indexPath.row == self.imagesArr.count - 1 ? NO : YES;
    [cell.nextButton addTarget:self action:@selector(changeVC:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.pages.currentPage = (int)(scrollView.contentOffset.x / kScreenWidth + 0.5);
}

- (void)changeVC:(UIButton *)sender {
    
    sender.userInteractionEnabled = NO;
    
    [UIView animateWithDuration:1 animations:^{
        self.collectionView.transform = CGAffineTransformMakeScale(2, 2);
        self.collectionView.alpha = 0;
        self.pages.alpha = 0;
    } completion:^(BOOL finished) {
        [CNNotificationCenter postNotificationName:CNNewVCDidChangeNotifacation object:nil userInfo:nil];
        [self setFirstStart];
    }];
}

/**
 *  保存用户是否为第一次登陆
 */
- (void)setFirstStart {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:isFirstStart];
}

@end
